package com.bigkoo.pickerview.adapter;


import com.contrarywind.adapter.WheelAdapter;

/**
 * Numeric Wheel adapter.
 */
public class NumericWheelAdapter implements WheelAdapter
{
    private int minValue;
    private int maxValue;
    private int interval;

    /**
     * Constructor
     *
     * @param minValue the wheel min value
     * @param maxValue the wheel max value
     */
    public NumericWheelAdapter(int minValue, int maxValue)
    {
        this.minValue = minValue;
        this.maxValue = maxValue;
        this.interval = 1;
    }

    public NumericWheelAdapter(int minValue, int maxValue, int interval)
    {
        this.interval = interval;
        this.maxValue = maxValue;
        if (interval == 1)
            this.minValue = minValue;
        else
            this.minValue = ((int) Math.ceil((double) minValue / (double) interval)) * interval;

        if (this.minValue >= this.maxValue)
            this.minValue = 0;
    }

    @Override
    public Object getItem(int index)
    {
        if (index >= 0 && index < getItemsCount())
            return ((minValue / interval) + index) * interval;
        return 0;
    }

    @Override
    public int getItemsCount()
    {
        return (int) Math.ceil((double) (maxValue - minValue + 1) / (double) interval);
    }

    @Override
    public int indexOf(Object o)
    {
        try
        {
            return ((int) o - minValue) / interval;
        }
        catch (Exception e)
        {
            return -1;
        }
    }
}
